package com.jifujun.fastdfs_springboot.service.impl;

import com.jifujun.fastdfs_springboot.client.FastDFSClient;
import com.jifujun.fastdfs_springboot.model.FastDFSProperties;
import lombok.extern.slf4j.Slf4j;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Create with IntelliJ IDEA
 * Project Name: fastdfs
 * Package Name: PACKAGE_NAME
 * Date: 2018/3/2710:07
 * User: 快乐的小菌菌
 * Description:
 */
@Component
@Scope("prototype")
@Slf4j
class FastDFSClientImpl implements FastDFSClient {
    private StorageClient storageClient;

    public FastDFSClientImpl(FastDFSProperties fastDFSProperties) {
        log.debug("获取配置文件路径：" + fastDFSProperties.getConfPath());
        try {
            ClientGlobal.init(fastDFSProperties.getConfPath());
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            storageClient = new StorageClient(trackerServer, null);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
    }

    /**
     * 以字节数组的形式上传文件
     *
     * @param bytes   字节数组
     * @param extName 后缀名，不带.
     * @return 存储路径名地址 返回null失败
     */
    @Override
    public String[] uploadFile(byte[] bytes, String extName) {
        String[] strings = null;
        try {
            strings = storageClient.upload_file(bytes, extName, null);
        } catch (IOException | MyException e) {
            e.printStackTrace();
            throw new RuntimeException("上传文件失败");
        }
        return strings;
    }

    /**
     * 根据文件路径上传文件
     *
     * @param path    文件路径
     * @param extName 后缀名，不带.
     * @return 存储路径名地址 返回null失败
     */
    @Override
    public String[] uploadFile(String path, String extName) {
        String[] strings = null;
        try {
            strings = storageClient.upload_file(path, extName, null);
        } catch (IOException | MyException e) {
            e.printStackTrace();
            throw new RuntimeException("上传文件失败");
        }
        return strings;
    }

    /**
     * 0 为成功，不是0为失败返回的是错误代码
     *
     * @param groupName      组名
     * @param remoteFileName 实际地址
     * @return 状态码
     */
    @Override
    public Integer deleteFile(String groupName, String remoteFileName) {
        Integer code = null;
        try {
            code = storageClient.delete_file(groupName, remoteFileName);
        } catch (IOException | MyException e) {
            throw new RuntimeException("删除文件失败");
        }
        return code;
    }

    /**
     * 下载
     *
     * @param groupName      组名
     * @param remoteFileName 实际地址
     * @return 字节数组，如果失败返回null
     */
    @Override
    public byte[] download(String groupName, String remoteFileName) {
        byte[] bytes = null;
        try {
            bytes = storageClient.download_file(groupName, remoteFileName);
        } catch (IOException | MyException e) {
            throw new RuntimeException("下载文件失败");
        }
        return bytes;
    }

}
